package com.fingard.dsp.bank.directbank.boc01;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.diagnostics.LogHelper;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.dsp.bank.directbank.GetBfTokenFromMaster;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.ReqB001OperBillQry;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.util.TransIDHelper;
import com.fingard.util.secret.salary.FieldHiddenUtil;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.fingard.io.FileHelper.writeFile;
import static com.fingard.text.StringHelper.trimEndFileSp;


public class BOC01Base extends DirectBase {

	protected String bankRetRightCode  = "B001";

	protected String swiftcodeFlag="BKCHCN";

	protected void saveBankReq(String obssid,String reqSeqID) throws Exception{
		String savePath =  getBankConfig().getValueAsString("obssidPath");
		savePath = trimEndFileSp(savePath) + File.separator;
		String fullName = savePath + reqSeqID + "_" + obssid;
		String content = reqSeqID + "_" + obssid;
		writeFile(fullName,content,"utf8");
	}

	protected boolean isNeedWaitFiveS(){
		boolean isNeedWait;
		String isNeedWaitStr = getBankConfig().getValueAsString("isNeedWait");
		isNeedWait = StringHelper.isNullOrEmpty(isNeedWaitStr) || "true".equals(isNeedWaitStr.toLowerCase());
		return isNeedWait;
	}


	protected String getBankSeq(String billNo){
		String msgSeq = "";
		String savePath =  trimEndFileSp(getBankConfig().getValueAsString("obssidPath"));
		File path = new File(savePath);
		for(File f : path.listFiles()){
			if(f.getName().contains(billNo)){
				try {
					msgSeq = f.getName().split("_")[0];
                    WriteBankLogLn("msgSeq = " + msgSeq);
					if(msgSeq != null && msgSeq.length() > 0){
					    return msgSeq;
                    }
				}
				catch (Exception ex){
					WriteBankLogLn(f.getName() + "文件内容出错，无法获取msgseq");
				}
			}
		}
		return msgSeq;
	}

	/*
	 * I1:票据查询I51:票据查验申请I52:票据查验接收I4:支付信用接收查复A2:提示承兑
	 * A3:承兑申请撤回A4:票据承兑A5:撤票申请A6:提示收票A7:提示收票撤回
	 * A8:收票确认B1:出票保证申请B2:承兑保证申请B3:背书保证申请B4:保证申请撤回
	 * B5:保证确认C1:背书转让C2:转让撤回C3:被背书人签收D1:贴现申请D2:贴现申请撤回
	 * E1:质押申请E2:质押申请撤回E3:质权确认E4:质权解除申请E5:质权解除申请撤回
	 * E6:质权解除确认F1:提示付款申请F2:提示付款申请撤回G1:票据付款确认H1:追索通知
	 * H2:追索通知撤回H3:同意清偿H4:同意清偿撤回H5:收到清偿确认
	 * */
	protected String  DealQueryType(ReqB001OperBillQry query){
		String QueryType = "";
		/// 业务类型 1-出票 2-承兑 3-收票 4-背书转让 5-质押 6-解质押 7-出票保证 8-承兑保证 9-背书保证 10-贴现 11-回购式贴现赎回 12-提示付款 13-追索 14-同意清偿
		//出票撤回 A5:撤票申请
		if("1".equals(query.detail.busiType)){
			if("3".equals(query.detail.queryType)){
				QueryType = "A5";
			}
		}

		if("2".equals(query.detail.busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(query.detail.queryType)){
				QueryType = "A2";
			}
			else if("2".equals(query.detail.queryType)){
				QueryType = "A4";
			}
			else{
				QueryType = "A3";
			}
		}
		//收票
		else if("3".equals(query.detail.busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(query.detail.queryType)){
				QueryType = "A6";
			}
			else if("2".equals(query.detail.queryType)){
				QueryType = "A8";
			}
			else{
				QueryType = "A7";
			}
		}
		//背书转让 C1:背书转让C2:转让撤回C3:被背书人签收
		else  if("4".equals(query.detail.busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(query.detail.queryType)){
				QueryType = "C1";
			}
			else if("2".equals(query.detail.queryType)){
				QueryType = "C3";
			}
			else{
				QueryType = "C2";
			}
		}
		//质押转让 E1:质押申请E2:质押申请撤回E3:质权确认
		else  if("5".equals(query.detail.busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(query.detail.queryType)){
				QueryType = "E1";
			}
			else if("2".equals(query.detail.queryType)){
				QueryType = "E3";
			}
			else{
				QueryType = "E2";
			}
		}
		//解质押  E4:质权解除申请E5:质权解除申请撤回  E6:质权解除确认
		else  if("6".equals(query.detail.busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(query.detail.queryType)){
				QueryType = "E4";
			}
			else if("2".equals(query.detail.queryType)){
				QueryType = "E6";
			}
			else{
				QueryType = "E5";
			}
		}
		//出票保证 B1:出票保证申请
		else  if("7".equals(query.detail.busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(query.detail.queryType)){
				QueryType = "B1";
			}
		}
		// 承兑保证 B2:承兑保证申请
		else  if("8".equals(query.detail.busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(query.detail.queryType)){
				QueryType = "B2";
			}
		}
		//背书保证 B3:背书保证申请
		else  if("9".equals(query.detail.busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(query.detail.queryType)){
				QueryType = "B3";
			}
		}
		//贴现 D1:贴现申请D2:贴现申请撤回
		else  if("10".equals(query.detail.busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(query.detail.queryType)){
				QueryType = "D1";
			}
			else{
				QueryType = "D2";
			}
		}
		//回购式贴现赎回 不支持
		else  if("11".equals(query.detail.busiType)){
		}
		//提示付款 F1:提示付款申请F2:提示付款申请撤回G1:票据付款确认
		else  if("12".equals(query.detail.busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(query.detail.queryType)){
				QueryType = "F1";
			}
			else if("2".equals(query.detail.queryType)){
				QueryType = "G1";
			}
			else{
				QueryType = "F2";
			}
		}
		//追索 H1:追索通知 H2:追索通知撤回
		else  if("13".equals(query.detail.busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(query.detail.queryType)){
				QueryType = "H1";
			}
			else{
				QueryType = "H2";
			}
		}
		//同意清偿 H3:同意清偿H4:同意清偿撤回H5:收到清偿确认
		else  if("14".equals(query.detail.busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(query.detail.queryType)){
				QueryType = "H3";
			}
			else if("2".equals(query.detail.queryType)){
				QueryType = "H5";
			}
			else{
				QueryType = "H4";
			}
		}
		return  QueryType;
	}

	protected String  getQueryType(String busiType,String queryType){
		String QueryType = "";
		/// 业务类型 1-出票 2-承兑 3-收票 4-背书转让 5-质押 6-解质押 7-出票保证 8-承兑保证 9-背书保证 10-贴现 11-回购式贴现赎回 12-提示付款 13-追索 14-同意清偿
		//出票撤回 A5:撤票申请
		if("1".equals(busiType)){
			if("3".equals(queryType)){
				QueryType = "A5";
			}
		}

		if("2".equals(busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(queryType)){
				QueryType = "A2";
			}
			else if("2".equals(queryType)){
				QueryType = "A4";
			}
			else{
				QueryType = "A3";
			}
		}
		//收票
		else if("3".equals(busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(queryType)){
				QueryType = "A6";
			}
			else if("2".equals(queryType)){
				QueryType = "A8";
			}
			else{
				QueryType = "A7";
			}
		}
		//背书转让 C1:背书转让C2:转让撤回C3:被背书人签收
		else  if("4".equals(busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(queryType)){
				QueryType = "C1";
			}
			else if("2".equals(queryType)){
				QueryType = "C3";
			}
			else{
				QueryType = "C2";
			}
		}
		//质押转让 E1:质押申请E2:质押申请撤回E3:质权确认
		else  if("5".equals(busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(queryType)){
				QueryType = "E1";
			}
			else if("2".equals(queryType)){
				QueryType = "E3";
			}
			else{
				QueryType = "E2";
			}
		}
		//解质押  E4:质权解除申请E5:质权解除申请撤回  E6:质权解除确认
		else  if("6".equals(busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(queryType)){
				QueryType = "E4";
			}
			else if("2".equals(queryType)){
				QueryType = "E6";
			}
			else{
				QueryType = "E5";
			}
		}
		//出票保证 B1:出票保证申请
		else  if("7".equals(busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(queryType)){
				QueryType = "B1";
			}
		}
		// 承兑保证 B2:承兑保证申请
		else  if("8".equals(busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(queryType)){
				QueryType = "B2";
			}
		}
		//背书保证 B3:背书保证申请
		else  if("9".equals(busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(queryType)){
				QueryType = "B3";
			}
		}
		//贴现 D1:贴现申请D2:贴现申请撤回
		else  if("10".equals(busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(queryType)){
				QueryType = "D1";
			}
			else{
				QueryType = "D2";
			}
		}
		//回购式贴现赎回 不支持
		else  if("11".equals(busiType)){
		}
		//提示付款 F1:提示付款申请F2:提示付款申请撤回G1:票据付款确认
		else  if("12".equals(busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(queryType)){
				QueryType = "F1";
			}
			else if("2".equals(queryType)){
				QueryType = "G1";
			}
			else{
				QueryType = "F2";
			}
		}
		//追索 H1:追索通知 H2:追索通知撤回
		else  if("13".equals(busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(queryType)){
				QueryType = "H1";
			}
			else{
				QueryType = "H2";
			}
		}
		//同意清偿 H3:同意清偿H4:同意清偿撤回H5:收到清偿确认
		else  if("14".equals(busiType)){
			/// 查询类型 1-申请 2-应答 3-撤回
			if("1".equals(queryType)){
				QueryType = "H3";
			}
			else if("2".equals(queryType)){
				QueryType = "H5";
			}
			else{
				QueryType = "H4";
			}
		}
		//赋值默认值I1
		if("".equals(QueryType)){
			QueryType = "I1";
		}
		return  QueryType;
	}

	protected String getBnkToken() {
		boolean isLoginValid = false;
		if(StringHelper.isNullOrEmpty(bankFront.token)){
			isLoginValid = true;
		} else if (bankFront.lastLoginTime == null) {
			isLoginValid = true;
		} else {
			int timeOut = getBankConfig().getValueAsInt("sessionMins");
			timeOut = timeOut <= 0 ? 15 : timeOut;
			Date currDate = new Date();
			if (DateHelper.getDifferMins(currDate, bankFront.lastLoginTime) > timeOut) {
				isLoginValid = true;
			} else if (DateHelper.compareDay(currDate, bankFront.lastLoginTime) != 0) {
				isLoginValid = true;
			}
		}
		if (getBankConfig().getValueAsBool("needLogin")) {
			if (isLoginValid) {
				WriteBankLogLn("当前未登录前置，现在进行登录");
				login();
			}
		}
		return bankFront.token;
	}
	
	protected void login(){
		try{
			if(bankFront.syncTokenFromMasterHost){
				WriteBankLogLn("从主控DSP服务器"+FGBiz.dspCenter.dspSet.masterServer+"同步会话");
				bankFront.lastLoginTime = new Date();
				bankFront.token = "";
				GetBfTokenFromMaster sync = new GetBfTokenFromMaster();
				int timeoutSec = getBankConfig().getValueAsInt("syncTokenTimeoutSec");
				HashMap<String,String> map = sync.execGetToken(this.bankFront,timeoutSec);
				WriteBankLogLn(sync.sbLog.toString());
				if("0".equals(map.get("respCode"))){
					bankFront.token = map.get("token");
					WriteBankLogLn("会话同步成功");
				}else{
					WriteBankLogLn("会话同步失败：" + map.get("respInfo"));
				}
			}else{
				int tmpMaxFailCount = getBankConfig().getValueAsInt("maxLoginFail");
				if(bankFront.loginFailCount>=tmpMaxFailCount){
					WriteBankLogLn("登录失败次数"+bankFront.loginFailCount+"已达到上限，不再进行登录");
					return;
				}
				bankFront.lastLoginTime = new Date();
				bankFront.token = "";
				XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
				//写入通用的头部节点
				xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\""+getCharset()+"\"?>");
				xmlWriter.writeStartElementByAttr("bocb2e", "<bocb2e version=\"100\" security=\"true\" lang=\"chs\">");
				xmlWriter.writeStartElement("head");
				xmlWriter.writeElementString("termid", bankFront.termid, true);//代表一台企业前置机
				xmlWriter.writeElementString("trnid", getQryTransID());//客户端产生的报文编号
				xmlWriter.writeElementString("custid", bankFront.org);
				xmlWriter.writeElementString("cusopr", bankFront.operator);
				xmlWriter.writeElementString("trncod", "b2e0001");
				xmlWriter.writeElementString("token", "");
				xmlWriter.writeEndElement();
				
				xmlWriter.writeStartElement("trans");
				xmlWriter.writeStartElement("trn-b2e0001-rq");
				xmlWriter.writeStartElement("b2e0001-rq");
				xmlWriter.writeElementString("custdt", Format.DateTime14Format.format(bankFront.lastLoginTime));
				xmlWriter.writeElementString("oprpwd", bankFront.loginPasswd);
				xmlWriter.writeEndDocument();
				String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
				if(tmpStrRet[0].length() == 0){
					StringBuilder tmpSbMsg = new StringBuilder();
					
					XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
					
					if(tmpBkReader != null){
						String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0001-rs/status/rspcod");
						String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0001-rs/status/rspmsg");
						if(tmprspcod.equals("B001")){
							bankFront.token = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0001-rs/token");
							bankFront.loginFailCount = 0;
							WriteBankLogLn("登录成功");
						}
						else{
							bankFront.loginFailCount=bankFront.loginFailCount+1;
							WriteBankLogLn("登录失败：" + tmprspmsg);
						}
					}
					else{
						WriteBankLogLn("登录失败");
					}
				}
				else{
					WriteBankLogLn("登录失败：" + tmpStrRet[1]);
				}
			}
			
		}catch(Exception e){
			WriteBankLogLn("登录异常：" + LogHelper.getStackTrace(e));
			e.printStackTrace();
		}
	}
	
	private static TransIDHelper idHelper = new TransIDHelper(1, 999999);
	/**12位：HHmmss+6位*/
	protected String getQryTransID(){
		Date tmpDateNow = new Date();
		//return Format.Time12Format.format(tmpDateNow);
		return Format.Time6Format.format(tmpDateNow)+idHelper.getNextIDStr();
	}
	
	/**20位：yyyyMMddHHmmss+6位*/
	protected String getTransID(){
		Date tmpDateNow = new Date();
		return Format.DateTime14Format.format(tmpDateNow)+idHelper.getNextIDStr();
	}
	
	protected String getCharset(){
		return getBankConfig().getCharset("GBK");
	}

	protected String getLocale(){
		return getBankConfig().getSetValue("locale","zh_CN","");
	}

	protected void writeCommonHeader(XmlTextWriter xmlWriter, BOC01Header p_bankHeader) throws Exception{
		if(StringHelper.isNullOrEmpty(p_bankHeader.trncod))
		{
			throw new Exception("未赋值trncod");
		}
		if(StringHelper.isNullOrEmpty(p_bankHeader.trnid))
		{
			throw new Exception("未赋值trnid");
		}
		//if(StringHelper.isNullOrEmpty(bankFront.org))
		//{
		//	throw new Exception("未赋值企业在中行网银系统的客户编码");
		//}
		//if(StringHelper.isNullOrEmpty(bankFront.operator))
		//{
		//	throw new Exception("未赋值企业操作员代码");
		//}
		xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\""+getCharset()+"\"?>");
		xmlWriter.writeStartElementByAttr("bocb2e", "<bocb2e version=\"100\" security=\"true\" lang=\"chs\" locale=\""+getLocale()+"\" >");
		xmlWriter.writeStartElement("head");
		xmlWriter.writeElementString("termid", bankFront.termid, true);//代表一台企业前置机
		xmlWriter.writeElementString("trnid", p_bankHeader.trnid);//客户端产生的报文编号
		if(StringHelper.hasAnyChar(p_bankHeader.orgOverride)){
			this.WriteBankLogLn("custid采用"+p_bankHeader.orgOverride);
			xmlWriter.writeElementString("custid", p_bankHeader.orgOverride);
		}else{
			xmlWriter.writeElementString("custid", bankFront.org);
		}
		xmlWriter.writeElementString("cusopr", bankFront.operator);
		xmlWriter.writeElementString("trncod", p_bankHeader.trncod);
		xmlWriter.writeElementString("token", getBnkToken());
		xmlWriter.writeEndElement();
		
		xmlWriter.writeStartElement("trans");
	}

	protected void writeCommonHeader2(XmlTextWriter xmlWriter, BOC01Header p_bankHeader) throws Exception{
		if(StringHelper.isNullOrEmpty(p_bankHeader.trncod))
		{
			throw new Exception("未赋值trncod");
		}
		if(StringHelper.isNullOrEmpty(p_bankHeader.trnid))
		{
			throw new Exception("未赋值trnid");
		}
		xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\""+getCharset()+"\"?>");
		xmlWriter.writeStartElementByAttr("bocb2e", "<bocb2e version=\"120\" locale=\"zh_CN\">");
		xmlWriter.writeStartElement("head");
		xmlWriter.writeElementString("termid", bankFront.termid, true);//代表一台企业前置机
		xmlWriter.writeElementString("trnid", p_bankHeader.trnid);//客户端产生的报文编号
		xmlWriter.writeElementString("custid", bankFront.org);
		xmlWriter.writeElementString("cusopr", bankFront.operator);
		xmlWriter.writeElementString("trncod", p_bankHeader.trncod);
		xmlWriter.writeElementString("token", getBnkToken());
		xmlWriter.writeEndElement();

		xmlWriter.writeStartElement("trans");
	}
	
	protected String[] sendToBank(String p_reqStr) throws Exception{
		String[] retStr = new String[]{"",""};
		
		WriteBankLogLn("2.提交银行的地址：" + bankFront.serverURL);
		if(isNeedHiddleField) {
			WriteBankLogStep2(FieldHiddenUtil.amountHiddle(p_reqStr, new String[]{"pybamt","pydamt"}));
		}else{
			WriteBankLogStep2(p_reqStr);
		}
		WebRequest tmpWebRequest = new WebRequest(bankFront.serverURL);
		tmpWebRequest.setConnectTimeout(60000);
		tmpWebRequest.setRequestProperty("Content-Type", "application/xml");
		tmpWebRequest.setRequestProperty("connection", "Close");
		String charsetName = getCharset();
		byte[] tmpToWriteBytes = p_reqStr.getBytes(charsetName);
		retStr = tmpWebRequest.upload(tmpToWriteBytes, charsetName);
		boolean enableSalaryDecrypt = getBankConfig().getValueAsBool("enableSalaryDecrypt");
		if(enableSalaryDecrypt) {
			WriteBankLogStep3(FieldHiddenUtil.amountHiddle(retStr[1],new String[]{"pybamt","pydamt"}));
		}else{
			WriteBankLogStep3(retStr[1]);
		}
		return retStr;
	}

	
	protected boolean isActiveRetCode(String retCode){
		if(retCode != null){
			if(retCode.equals("B107")){//B107=TOKEN错误或未登录
				bankFront.token = "";//需要重新登录
			}
			if(retCode.equals("B001") || retCode.equals("B002") || retCode.equals("B003") || retCode.equals("B054") || retCode.equals("B500") || retCode.equals("B028")){
				bankFront.lastVisitTime = new Date();
				return true;
			}
		}
		return false;
	}

	//上送格式为json格式
	protected  String[] sendToATS(String jsonData) throws Exception{
		String[] retStr = new String[]{"",""};
		WriteBankLogLn("2.提交ATS的地址："+ FGBiz.dspCenter.dspSet.receiptPushServerUrl);


		//发送ATS
		WebRequest tmpRequest = new WebRequest(FGBiz.dspCenter.dspSet.receiptPushServerUrl);
		tmpRequest.setConnectTimeout(60000);
		tmpRequest.setRequestProperty("connection", "Close");
		tmpRequest.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
		String charsetName = getCharset();
		WriteBankLogLn("3.推送给ATS的json数据"+jsonData);
		byte[] tmpWriteBytes = jsonData.getBytes(charsetName);
		retStr = tmpRequest.upload(tmpWriteBytes, charsetName);
		WriteBankLogLn("4.返回的数据"+retStr[1]);

		return retStr;
	}

	//上送格式为json格式
	protected  String[] sendToATSIsCode(String jsonData) throws Exception{
		String[] retStr = new String[]{"",""};
		WriteBankLogLn("2.提交ATS的地址："+ FGBiz.dspCenter.dspSet.codePushServerUrl);
		//发送ATS
		WebRequest tmpRequest = new WebRequest(FGBiz.dspCenter.dspSet.codePushServerUrl);
		tmpRequest.setConnectTimeout(600000);
		tmpRequest.setRequestProperty("connection", "Close");
		tmpRequest.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
		String charsetName = getCharset();
		WriteBankLogLn("3.推送给ATS的json数据"+jsonData);
		byte[] tmpWriteBytes = jsonData.getBytes(charsetName);
		retStr = tmpRequest.upload(tmpWriteBytes, charsetName);
		WriteBankLogLn("4.返回的数据"+retStr[1]);

		return retStr;
	}

	public Map<String,Req7506BalQry> getReq7506QryMap(Req7506BalQry req7506BalQry) throws Exception{
		Map<String,Req7506BalQry> map=new HashMap<String, Req7506BalQry>();
		if (req7506BalQry.isBatch) {
			XmlTextWriter xmlReq = new XmlTextWriter();//境内请求报文
			XmlTextWriter xmlReq2 = new XmlTextWriter();//境外请求报文

			xmlReq.writeStartElement("ATSYH");
			xmlReq.writeStartElement("TransReq");
			xmlReq2.writeStartElement("ATSYH");
			xmlReq2.writeStartElement("TransReq");

			String req=req7506BalQry.getOriReqXml();
			String req7506Bal=req.substring(req.indexOf("<TransReq>")+"<TransReq>".length(),req.indexOf("<TransParam>"));

			xmlReq.directWrite(req7506Bal);
			xmlReq2.directWrite(req7506Bal);

			xmlReq.writeStartElement("TransParam");
			xmlReq2.writeStartElement("TransParam");

			XmlTextReader tmpBkReader = loadBankRespXmlReader(req7506BalQry.getOriReqXml(), new StringBuilder());
			while (tmpBkReader.read(req7506BalQry.PATH_Detail)){
				int start=req.indexOf("<DetailRecord>");
				int end=req.indexOf("</DetailRecord>")+"</DetailRecord>".length();
				String detailRecord=req.substring(start,end);

				String swiftcode=tmpBkReader.readedXElement.getSingleTextNotNull("swiftcode");
				if(StringHelper.hasAnyChar(swiftcode) && !swiftcodeFlag.equals(swiftcode.substring(0,6))){
					xmlReq2.directWrite(detailRecord);
				}else {
					xmlReq.directWrite(detailRecord);
				}

				req=req.replace(detailRecord,"");
			}

			xmlReq.writeEndDocument();
			xmlReq2.writeEndDocument();

			Req7506BalQry req7506BalQry2=new Req7506BalQry(new XmlTextReader(xmlReq.toXmlString()), req7506BalQry.reqHeader);
			Req7506BalQry req7506BalQry3=new Req7506BalQry(new XmlTextReader(xmlReq2.toXmlString()), req7506BalQry.reqHeader);

			if(req7506BalQry2.getOriReqXml().contains("DetailRecord")){
				map.put("0",req7506BalQry2);//境内
			}
			if(req7506BalQry3.getOriReqXml().contains("DetailRecord")){
				map.put("1",req7506BalQry3);//境外
			}
		}else {
			XmlTextReader tmpBkReader = loadBankRespXmlReader(req7506BalQry.getOriReqXml(), new StringBuilder());
			while (tmpBkReader.read("/ATSYH/TransReq/TransParam")){
				String swiftcode=tmpBkReader.readedXElement.getSingleTextNotNull("swiftcode");
				if(StringHelper.hasAnyChar(swiftcode) && !swiftcodeFlag.equals(swiftcode.substring(0,6))){
					map.put("1",req7506BalQry);//境外
				}else {
					map.put("0",req7506BalQry);//境内
				}
			}
		}
		return map;
	}

	/**
	 * 中文映射
	 * @param content
	 * @return
	 * @throws Exception
	 */
	protected String getEnglishByCN(String content) throws Exception{
		String result=getBankConfig().getMapValueByKey("recInfomationMapping",content);
		if(StringHelper.isNullOrEmpty(result)){
			result=content;
		}
		return result;
	}
}
